/* eslint-disable react/jsx-key */
import React, { useState, useRef, useMemo } from 'react';
import ReactDOM from 'react-dom';
import { Card, Switch, Button, Modal, message, Popconfirm } from 'antd';
import AutoTable from '@/components/AutoTable';
import InitForm from '@/components/InitForm';
import { deleteinput_tax } from '@/services/business';
import { connect } from 'umi';
import { PlusOutlined } from '@ant-design/icons';
import { company } from '@/services/business';
import { getFetch } from '@/utils/doFetch';
import CompanyName from '@/components/CompanyName';

// type 类型有 table treeselect upload inputnumber datepicker daterange radio select textarea autoinput editor password input

function Inputtax(props) {
  let defaultFields = {
    company_id: {
      value: null,
      type: 'select',
      title: '代账公司',
      name: ['company_id'],
      required: true,
      options: {
        database: company,
        params: {
          is_all: 1,
          type: -1,
          status: 'completed',
        },
      },
      col: { span: 24 },
    },
    invoice_date: {
      value: null,
      type: 'datepicker',
      title: '开票日期',
      name: ['invoice_date'],
      required: true,
      col: { span: 24 },
    },
    input_tax_amount: {
      value: null,
      type: 'inputnumber',
      title: '进项税额',
      name: ['input_tax_amount'],
      required: true,
      col: { span: 24 },
    },
  };
  const [vs, cvs] = useState(false), //表单显/隐
    [fields, cf] = useState(defaultFields),
    [iftype, ciftype] = useState({});

  let actionRef = useRef(),
    ChildRef = null;
  function reload() {
    actionRef?.current?.reload();
    ChildRef?.onRefresh();
  }
  const columns = useMemo(() => {
    return [
      {
        title: '公司名称',
        width: 260,
        dataIndex: 'company_name',
        key: 'company_name',
        render: (_, row) => (
          <CompanyName
            row={{ ...row, name: row?.company_name ?? row.name, id: row?.company_id ?? row.id }}
            reload={reload}
          />
        ),
      },
      {
        title: '编号',
        dataIndex: 'company_code',
        key: 'company_code',
        search: false,
      },
      {
        title: '公司类型',
        dataIndex: 'classify_name',
        key: 'classify_name',
        search: false,
      },
      {
        title: '开票日期',
        dataIndex: 'invoice_date',
        key: 'invoice_date',
        search: false,
      },
      {
        title: '进项税额',
        dataIndex: 'input_tax_amount',
        key: 'input_tax_amount',
        search: false,
      },
      {
        title: '操作',
        valueType: 'option',
        width: 100,
        render: (text, record, _, action) => [
          <a
            style={{ color: '#1890ff' }}
            onClick={() => {
              cvs(true);
              cf((fields) => {
                for (let i in fields) {
                  fields[i].value = record[i];
                }
                return { ...fields };
              });
              ciftype({
                val: 'edit',
                title: '编辑进项税额',
                id: record.id,
              });
            }}
          >
            编辑
          </a>,
          <Popconfirm
            placement="bottom"
            title={'确认删除该进项税额？'}
            onConfirm={() => {
              deleteinput_tax(record.id).then((res) => {
                if (res.code == 0) {
                  message.success('操作成功');
                  reload();
                }
              });
            }}
            okText="删除"
            cancelText="取消"
          >
            <a style={{ color: '#f50' }}>删除</a>
          </Popconfirm>,
        ],
      },
    ];
  }, []);

  let extrarender = [
    <div
      className="action"
      title="新增"
      onClick={() => {
        cvs(true);
        cf((fields) => {
          for (let i in fields) {
            fields[i].value = null;
          }
          return { ...fields };
        });
        ciftype({
          val: 'add',
          title: '新增进项税额',
        });
      }}
    >
      <PlusOutlined />
    </div>,
  ];

  let saveData = (values) => {
    let { dispatch } = props;
    if (iftype.val == 'add') {
      dispatch({
        type: 'business/addinput_tax',
        payload: values,
      }).then((res) => {
        if (res.code == 0) {
          message.success('操作成功');
          reload();
          cvs(false);
        }
      });
    } else if (iftype.val == 'edit') {
      dispatch({
        type: 'business/editinput_tax',
        payload: { ...values, id: iftype.id },
      }).then((res) => {
        if (res.code == 0) {
          message.success('操作成功');
          reload();
          cvs(false);
        }
      });
    }
  };

  return (
    <div className="pgcontainer">
      <AutoTable
        pagetitle={props.route.name} //页面标题
        pageextra={extrarender} //页面操作 新增or批量删除
        columns={columns}
        actionRef={actionRef}
        onRef={(node) => (ChildRef = node)}
        path="/wbn-api/input_tax"
        //withCard={false}
        //cardtitle = "name" //卡片标题
        //cardavatar = "img_url" //卡片头像
      ></AutoTable>
      <Modal
        maskClosable={false}
        title={iftype.title}
        open={vs}
        onCancel={() => cvs(false)}
        footer={false}
        destroyOnClose={true}
      >
        <InitForm
          fields={fields}
          submitData={(values) => {
            saveData(values);
          }}
          onChange={(changedValues, allValues) => {
            //联动操作
          }}
          submitting={
            props.loading.effects['business/addinput_tax'] ||
            props.loading.effects['business/editinput_tax'] ||
            !vs
          }
        ></InitForm>
      </Modal>
    </div>
  );
}

export default connect(({ business, loading, user }) => ({
  business,
  loading,
  user,
}))(Inputtax);
